﻿#region Disclaimer / License
// Copyright (C) 2011, Kenneth Bergeron, IAP Worldwide Services, Inc
// NOAA :: National Marine Fisheries Service
// 
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
// 
#endregion
using System;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace CatchProcessing
{
    /// <summary>
    /// Show a summary report
    /// </summary>
    public partial class SummaryTreeForm : Form
    {
        /// <summary>
        /// The XML dataset
        /// </summary>
        private DataSets.LocalDataSet LocalDataSet;
        /// <summary>
        /// Show a summary report
        /// </summary>
        public SummaryTreeForm()
        {
            InitializeComponent();
            this.Text += "   " + Properties.Settings.Default.VesselName;
            this.CruiseTextBox.Text = Properties.Settings.Default.Cruise;
            this.treeColumnsView1.NodeDoubleClicked += new TreeNodeMouseClickEventHandler(treeColumnsView1_NodeDoubleClicked);
            this.LocalDataSet = new DataSets.LocalDataSet();
            this.LocalDataSet.Load();
        }
        /// <summary>
        /// Double click a node will bring up viewer
        /// </summary>
        void treeColumnsView1_NodeDoubleClicked(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (!e.Node.Text.Contains(Sep)) return;
            string[] Parts = e.Node.Text.Split(new string[] { Sep }, StringSplitOptions.None);
            DataSets.LocalDataSet.FishRow Row = this.LocalDataSet.Fish.FindByStationHookID(Parts[0], int.Parse(Parts[1]));
            new CatchDialog(Row).ViewDialog();
        }
        // Seperates the statin from the hooks
        private const string Sep = ", ID:";
        /// <summary>
        /// Loaded
        /// </summary>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            // Put up wait indicator
            using (Utility.WaitDialog wd = new Utility.WaitDialog())
            {
                wd.Show("Loading...");
                this.treeColumnsView1.Suspend();
                // Load the tree
                foreach (TreeNode RootNode in
                    from DataSets.LocalDataSet.FishRow qR in this.LocalDataSet.Fish
                    where qR.Cruise == Properties.Settings.Default.Cruise && qR.VesselCode == Properties.Settings.Default.VesselCode 
                    group qR by qR.Species into G
                    select new TreeNode(G.Key)
                    {   // Root nodes are average values
                        Tag = new object[] { "Average", G.Average(qR => qR.Weight), G.Average(qR => qR.LenTotal), null, null, null }
                    })
                {
                    // Load the leaf nodes
                    foreach (DataSets.LocalDataSet.FishRow Row in this.LocalDataSet.Fish.Where(qR => qR.Species == RootNode.Text))
                    {
                        RootNode.Nodes.Add(new TreeNode(Row.Station + Sep +Row.HookID)
                        {
                            Tag = new object[] 
                            {   // Leaf nodes are actual values
                                "  ", Row.Weight, Row.LenTotal, Row.SexName, Row.MortalityName, Row.StageName, string.Empty
                            }
                        });
                    }
                    this.treeColumnsView1.Nodes.Add(RootNode);
                }
                this.treeColumnsView1.Resume();
            }

        }
        /// <summary>
        /// Draw empty cells if values are null
        /// </summary>
        private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.RowIndex < 0 || e.ColumnIndex < 0 || e.Value != null) return;
            e.Graphics.FillRectangle(Brushes.LightGray, e.CellBounds);
            e.Graphics.DrawLine(Pens.Black, e.CellBounds.Right-1, e.CellBounds.Bottom, e.CellBounds.Right-1, e.CellBounds.Top);
            e.Handled = true;
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {

        }
    }
}
